package com.easonji.admin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.easonji.admin.manager.UserManager;
import com.easonji.admin.query.OperationLogQuery;
import com.easonji.admin.service.OperationLogService;
import com.easonji.core.util.ContrastUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.easonji.admin.mapper.OperationLogMapper;
import com.easonji.admin.model.OperationLog;
import com.easonji.common.base.service.AbstractBaseService;
import com.easonji.common.constant.enums.BizLogTypeEnum;
import com.easonji.common.factory.PageFactory;
import com.easonji.common.page.PageInfo;
import com.easonji.common.util.JsonUtils;
import com.easonji.common.util.StringUtils;

/**
 * @ClassName: OperationLogService
 * @Description: 业务日志服务实现类
 * @Author: LiuJun
 * @date: 2018/07/30 16:11:12
 * @Version: 1.0
 * @Copyright: Copyright (c) 2018 - 2019
 */
@Service
public class OperationLogServiceImpl extends AbstractBaseService<OperationLog> implements OperationLogService {

    @Resource
    private OperationLogMapper operationLogMapper;

    @Resource
    private UserManager userManager;

    @Override
    public Object queryOperationLogList(OperationLogQuery operationLogQuery) {
        // 分页信息
        Page<OperationLog> page = new PageFactory<OperationLog>().defaultPage();
        // 查询条件
        Wrapper<OperationLog> wrapper = new EntityWrapper<>();
        String beginTime = operationLogQuery.getBeginTime();
        if (StringUtils.isNotEmpty(beginTime)) {
            wrapper.ge("gmt_create", beginTime);
        }
        String endTime = operationLogQuery.getEndTime();
        if (StringUtils.isNotEmpty(endTime)) {
            wrapper.le("gmt_create", endTime);
        }
        String condition = operationLogQuery.getCondition();
        if (StringUtils.isNotEmpty(condition)) {
            wrapper.like("log_name", condition);
        }
        Integer logType = operationLogQuery.getLogType();
        if (StringUtils.isNotEmpty(logType) && logType != 0) {
            wrapper.eq("log_type", BizLogTypeEnum.valueOf(logType));
        }
        // 排序
        wrapper.orderBy("gmt_create");
        List<OperationLog> operationLogs = operationLogMapper.selectPage(page, wrapper);
        return new PageInfo(warp(operationLogs), page.getTotal());
    }

    @Override
    public Boolean removeOperationLog() {
        Wrapper<OperationLog> wrapper = new EntityWrapper<>();
        wrapper.ge("id", 0);
        return operationLogMapper.delete(wrapper) > 0 ? true : false;
    }

    @Override
    protected JSONObject warp2Json(OperationLog operationLog) {
        JSONObject result = JsonUtils.parseObject(operationLog);
        result.put("userName", userManager.getUserNameById(operationLog.getUserId()));
        String message = operationLog.getMessage();
        // 如果信息过长,则只截取前100位字符串
        int msgMaxLength = 100;
        if (StringUtils.isNotEmpty(message) && message.length() >= msgMaxLength) {
            String subMessage = message.substring(0, 100) + "...";
            result.put("message", subMessage);
        }
        // 如果信息中包含分割符号;;; 则分割字符串返给前台
        if (StringUtils.isNotEmpty(message) && message.indexOf(ContrastUtils.SEPARATOR) != -1) {
            String[] msgs = message.split(ContrastUtils.SEPARATOR);
            result.put("regularMessage", msgs.toString());
        } else {
            result.put("regularMessage", message);
        }
        return result;
    }
}
